package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.sql.Date;
import java.util.Calendar;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.Employee;
import DataStorageClasses.Job;
import DataStorageClasses.TimeSheet;
import Database.DatabaseConnector;

@WebServlet("/EditTimeSheetServlet")
public class EditTimeSheetServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	{
		DatabaseConnector db = new DatabaseConnector();
		String business = (String) request.getSession().getAttribute("username");
		long jobID = Long.parseLong(request.getParameter("jobID"));
		long empID = Long.parseLong(request.getParameter("empID"));
		String dateString = request.getParameter("date");
		double hours = -1;
		Boolean payed = Boolean.parseBoolean( request.getParameter("payed"));
		Date date = null;
		
		try {
			try {
				hours = Double.parseDouble(request.getParameter("hours"));
			}
			catch (NumberFormatException e) {
				Job job = db.getJob(jobID);
				JSONObject result = job.getJSONObject();
				result.put("failed", true);
				result.put("message", "Hours Must Be Numbers");
				
				response.getWriter().print(result);
				response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
				return;
			}
			catch(NullPointerException e) {
				Job job = db.getJob(jobID);
				JSONObject result = job.getJSONObject();
				result.put("failed", true);
				result.put("message", "Hours Must Be Numbers");
				
				response.getWriter().print(result);
				response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
				return;
			}
			try {
				Calendar calendar = Calendar.getInstance();
				if(dateString != null) {
					String[] dateArray = dateString.split("-");
					calendar.set(Integer.parseInt(dateArray[0]), Integer.parseInt(dateArray[1])-1, Integer.parseInt(dateArray[2]));
					date = new Date(calendar.getTimeInMillis());
				}
			}
			catch (NumberFormatException e) {
				Job job = db.getJob(jobID);
				JSONObject result = job.getJSONObject();
				result.put("failed", true);
				result.put("message", "Incorrect Date Format");
				
				response.getWriter().print(result);
				response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
				return;
			}
			catch(NullPointerException e) {
				Job job = db.getJob(jobID);
				JSONObject result = job.getJSONObject();
				result.put("failed", true);
				result.put("message", "Incorrect Date Format");
				
				response.getWriter().print(result);
				response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
				return;
			}
			
			if(db.editTimeSheet(jobID, empID, date, hours, payed)) {
				TimeSheet sheet = db.getTimeSheet(jobID, empID, date);
				Employee employee = db.getEmployee(empID, business);
				JSONObject result = new JSONObject();
				result.put("timeSheet", sheet.getJSONObject());
				result.put("employee", employee.getJSONObject());
				result.put("failed", false);
				response.getWriter().print(result);
			}
		}catch(JSONException e) { 
			e.printStackTrace();
		} catch(IOException e) {
			e.printStackTrace();
		}
		
		response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
	}
}
